(function(_,$){
	if(!_.Changer){
		return ;
	}
	
	/**
	 * 桌面快捷方式
	 */
	_.Changer.addHandler({
		name : "link",
		selector : "div[changer='link']",
		handler : function(objs){
			objs.each(function(i){
				var current = objs.eq(i);
				var container = current.parent();
				var ctnWidth = container.width();
				var ctnHeight = container.height();
				var config = current.attr("config");
				if(config){ config = eval("("+config+")"); }else{ config = {}; }	//转换config
				var label = current.html();
				if(label){config.label = label;}
				var link = _.link(config);
				link.display();
				link.jQuery.insertAfter(current);
				var target = current.attr("target");
				
				var clone = undefined;
				var layerY = 0;
				var layerX = 0;
				var startTop = 0;
				var startLeft = 0;
				/**
				 * 图标拖动事件
				 */
				link.jQuery.draggable({
					containment : "parent",
					addClasses: false,
					scroll : false,
					grid : [80,80],
					start : function(e){
						if($.browser.msie || $.browser.opera){
							layerY = e.offsetY;
							layerX = e.offsetX;
						}else{
							layerY = e.layerY;
							layerX = e.layerX;
						}
						
						startTop = link.jQuery.get(0).offsetTop;
						startLeft = link.jQuery.get(0).offsetLeft;
						
						clone = link.jQuery.clone();
						clone.css({
							"z-index":100000,
							position : "absolute"
						});
						clone.appendTo($("body"));
						link.jQuery.hide();
					},
					drag : function(e){
						clone.css({
							"top" : e.pageY - layerY,
							"left" : e.pageX - layerX
						});
						link.jQuery.attr("jdmer_top",link.jQuery.css("top").replace("px",""));
						link.jQuery.attr("jdmer_left",link.jQuery.css("left").replace("px",""));
					},
					stop : function(e){
						clone.remove();
						var container = link.jQuery.parent();
						var currentTop = link.jQuery.attr("jdmer_top");
						var currentLeft = link.jQuery.attr("jdmer_left");
						var existLink = container.children("[jdmer_uid!='"+link.uid+"'][jdmer_top='"+currentTop+"'][jdmer_left='"+currentLeft+"']");
						if(existLink.size() > 0){	//当前位置有图标
							existLink.css({
								top : startTop,
								left : startLeft
							}).attr({
								"jdmer_top" : startTop,
								"jdmer_left" : startLeft
							});
						}
						link.jQuery.show();
					}
				});
				
				if(target){
					var windowConfig = current.attr(target);
					if(windowConfig){ windowConfig = eval("("+windowConfig+")"); }else{ windowConfig = {}; }	//转换windowConfig
					
					windowConfig.title = config.label;
					if(windowConfig.container == undefined){
						windowConfig.container = container;
					}
					windowConfig.onDisplay = function(_win){
						_.desktop.taskbar.addItem(_win);
					};
					windowConfig.onDestroy = function(_win){
						var w = _.Window.getUnderTop(_win.container());
						if(w) w.toTop();
						_.desktop.taskbar.removeItem(_win);
					};
					windowConfig.onTitleChange = function(_win){
					};
					windowConfig.onToTop = function(_win){
						if(!_win.jQuery.is(":hidden")){
							_.desktop.taskbar.switchItem(_win);
						}
					};
					windowConfig.onMin = function(_win){
						var w = _.Window.getUnderTop(_win.container());
						if(w) w.toTop();
					};
					
					var win = {
						jQuery : null
					};
					link.jQuery.dblclick(function(){
						if(win.jQuery == null){
							win = _.window(windowConfig);
							ctnWidth = container.width();
							ctnHeight = container.height();
							win.top((ctnHeight - win.height())/2);
							win.left((ctnWidth - win.width())/2);
						}
						win.display();
					});
				}
				
				current.remove();
			});
		}
	});
	
	/**
	 * form的ajax提交支持
	 */
	_.Changer.addHandler({
		name : "form_ajax",
		selector : "form[changer*='ajax']",
		handler : function(objs , region){
			objs.each(function(i){
				var cur = objs.eq(i);
				cur.submit(function(){
					if(cur.hasClass("jdmer_validate")){
						if(!cur.valid()){
							return false;
						}
					}
					var url = undefined;
					if(cur.attr("action")){
						url = cur.attr("action");
					}
					var type = "post";
					if(cur.attr("method")){
						type = cur.attr("method");
					}
					
					var config = cur.attr("config");
					if(config){ config = eval("("+config+")"); }else{ config = {}; }	//转换config
					
					_.AnimateLayer.open();
					$.ajax({
						url: url,
						type : type,
						dataType: "html",
						data: cur.serializeArray(),
						// 回调函数
						complete:function(jqXHR, textStatus){
							_.AnimateLayer.close();
							var data = jqXHR.responseText;
							if(config.callback){
								config.callback(data);
							}
						}
					});
					return false;
				});
			});
		}
	});
	
	/**
	 * 超链接的window方式打开
	 */
	_.Changer.addHandler({
		name : "a_target_window",
		selector : "a[target='window']",
		handler : function(objs , region){
			objs.each(function(i){
				var current = objs.eq(i);
				var config = current.attr("config");
				if(config){ config = eval("("+config+")"); }else{ config = {}; }	//转换config
				
				var windowConfig = current.attr("window");
				if(windowConfig){ windowConfig = eval("("+windowConfig+")"); }else{ windowConfig = {}; }	//转换windowConfig
				
				var container = $("body");
				if(windowConfig.container){
					container = $(windowConfig.container);
				}
				
				
				var win = {
					jQuery : null
				};
					
				current.click(function(){
					if(windowConfig.url == undefined){
						windowConfig.url = current.attr("href");
					}
					if(windowConfig.title == undefined){
						windowConfig.title = current.text();
					}
					
					if(config.taskbar){	//如果需要在任务栏显示
						windowConfig.onDisplay = function(_win){
							_.desktop.taskbar.addItem(_win);
						};
						windowConfig.onDestroy = function(_win){
							var w = _.Window.getUnderTop(_win.container());
							if(w) w.toTop();
							_.desktop.taskbar.removeItem(_win);
						};
						windowConfig.onToTop = function(_win){
							if(!_win.jQuery.is(":hidden")){
								_.desktop.taskbar.switchItem(_win);
							}
						};
						windowConfig.onMin = function(_win){
							var w = _.Window.getUnderTop(_win.container());
							if(w) w.toTop();
						};
					}else{
						windowConfig.onDisplay = null;
						windowConfig.onDestroy = null;
						windowConfig.onToTop = null;
						windowConfig.onMin = null;
					}
					
					if(config.once){	//如果只能打开一次
						if(win.jQuery == null){
							win = _.window(windowConfig);
							ctnWidth = container.width();
							ctnHeight = container.height();
							win.top((ctnHeight - win.height())/2);
							win.left((ctnWidth - win.width())/2);
						}
						win.display();
					}else{
						//创建window
						win = _.window(windowConfig);
						ctnWidth = container.width();
						ctnHeight = container.height();
						win.top((ctnHeight - win.height())/2);
						win.left((ctnWidth - win.width())/2);
						win.display();
					}
					
					return false;
				});
			});
		}
	});
})(jDreamer,jQuery);